/**
 * 
 */
package edu.towson.cosc.cosc455.bmoran.compiler;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;


public class MyLexicalAnalyzer implements LexicalAnalyzer {
	
	String nextCharacter = "";
	int position =0;
	
	@Override
	/**
	 * Gets next token in the file
	 */
	public void getNextToken() {
		getCharacter();
		while (isSpace(MyCompiler.token)){
			getCharacter();
		}
		if (MyCompiler.token.equals("#")){
			processTag();
			while (MyCompiler.token.endsWith(" ")){
				MyCompiler.token = MyCompiler.token.substring(0,MyCompiler.token.length()-1);
			}
			if(!lookupToken()){
				System.out.println("LEXICAL ERROR! " + MyCompiler.token + " is not a valid token");
				System.exit(0);
			}
		}
		else{
			processText();
			if(MyCompiler.token.startsWith(" "))
				MyCompiler.token = MyCompiler.token.substring(1,  MyCompiler.token.length());
		}
		
	
		}
			
		
	

	
	@Override
	/**
	 * gets the character in the token until the file is empty then it exits
	 */
	public void getCharacter() {
		if ((position +1) > MyCompiler.fileString.length()){
			System.err.println("The file is empty.. exiting program");
			System.exit(0);
		}
		char currentCharacter = MyCompiler.fileString.charAt(position);
		MyCompiler.token = currentCharacter + "";
		position++;
	}
		
	@Override
	public void addCharacter() {
		if((position+1)> MyCompiler.fileString.length()){
			System.err.println("Lexical Error! "+ MyCompiler.token + "is not valid");
			System.exit(0);
		}
		
		char currentCharacter = MyCompiler.fileString.charAt(position);
		nextCharacter = currentCharacter + "";
		position++;
	}

	@Override
	/**checks for spaces
	 * 
	 */
	public boolean isSpace(String c){
		if (c.equals(" ") || c.equals("\t") || c.equals("\r"))
			return true;
		else
			return false;
	}
	
	
	@Override
	/**
	 * looksup token to make sure its valid in the goodTokens arraylist
	 */
	public boolean lookupToken() {
		for( int i=0; i < (MyCompiler.goodTokens).size(); i++){
			if((MyCompiler.token).equalsIgnoreCase( MyCompiler.goodTokens.get(i))){
				return true;
				
			}
		}
			return false;
	}
/**
 * if the token doesnt start with a hastag then it is text and must call addcharacter
 */
	public void processText(){
	while (!nextCharacter.equals("#") && (position+1)  <= MyCompiler.fileString.length()){
			addCharacter();
			if(!nextCharacter.equals("#")){
				MyCompiler.token+=nextCharacter;
			}
		}
		
		if(nextCharacter.equals("#")){
			position--;
		}
	}
	/**
	 * Processes a one word tag
	 */
	public void processTag(){
		addCharacter();
		while (!isSpace(nextCharacter) && (!nextCharacter.equals("#")) && (position+1)  <= MyCompiler.fileString.length()){
			MyCompiler.token += nextCharacter;
			addCharacter();
		}
		
		if((position+1) > MyCompiler.fileString.length()){
			MyCompiler.token+=nextCharacter;
		}else{
			if(nextCharacter.equalsIgnoreCase("#")){
					position--;
		}
		/**
		 * if more than one word tries calling the two word tag
		 */
		if(!lookupToken()){
			processTwoTag();
		}
		/**
		 * if more than two words tries calling three word tag
		 */
		if(!lookupToken()){
			processThreeTag();
		}
		}
		
			
		}
		
	
	
	public void processTwoTag(){
		MyCompiler.token +=nextCharacter;
		addCharacter();
		while (!(isSpace(nextCharacter)) && (position+1)  <= MyCompiler.fileString.length()){
			MyCompiler.token += nextCharacter;
		addCharacter();
		}
		if((position+1) > MyCompiler.fileString.length()){
			MyCompiler.token+=nextCharacter;
		}else { 
				if(nextCharacter.equals("#")){
					position--;
					
				}
			}
		
		
	}
	public void processThreeTag(){
		
		MyCompiler.token +=nextCharacter;
		addCharacter();
		while (!(isSpace(nextCharacter)) && (position+1)  <= MyCompiler.fileString.length()){
			MyCompiler.token += nextCharacter;
		addCharacter();
		}
		if((position+1) > MyCompiler.fileString.length()){
			MyCompiler.token+=nextCharacter;
		}else { 
			if(nextCharacter.equals("#")){
				position--;
				
			}
		}
	}
	

public boolean endCheck(){
	while((position+1) <= MyCompiler.fileString.length()){
		addCharacter();
		if(!isSpace(nextCharacter)){
			return false;
		}
	}
	return true;
	
		}


}

